package com.unieagle.exam.model;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import android.os.Parcel;
import android.os.Parcelable;

public class Question implements Parcelable{

	private long id;
	
	private long subjectId;
	
	private long caseId;
	
	private String type;
	
	private String typeName;
	
	private int required;
	
	private String content;
	
	private String update_status;
	
	private List<Answer> answer; //正确答案
	
	private List<Answer> choose; //考生选择的答案
	
	public Question() {
		id = 0;
		answer = new ArrayList<Answer>();
		choose = new ArrayList<Answer>();
	}
	
	public boolean isBlank() {
		return id == 0;
	}
	
	public static Question parse(JSONObject obj) {
		Question q = new Question();
		q.setId(obj.optLong("id"));
		q.setSubjectId(obj.optLong("subject_id"));
		q.setContent(obj.optString("title"));
		q.setType(obj.optString("question_type"));
		q.setCaseId(obj.optLong("case_id"));
		q.setUpdateStatus(obj.optString("update_status"));
		if(obj.optBoolean("is_required")) {
			q.setRequired(1);
		} else {
			q.setRequired(0);
		}
		JSONArray arr = obj.optJSONArray("question_items");
		if(arr != null) {
			for(int i = 0; i < arr.length(); i++) {
				Answer ans = Answer.parse(arr.optJSONObject(i));
				ans.setQuestionId(q.getId());
				q.answer.add(ans);
			}
		}
		return q;
	}
	
	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public long getSubjectId() {
		return subjectId;
	}

	public void setSubjectId(long subjectId) {
		this.subjectId = subjectId;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public List<Answer> getAnswer() {
		return answer;
	}

	public void setAnswer(List<Answer> answer) {
		this.answer = answer;
	}

	public List<Answer> getChoose() {
		return choose;
	}

	public void setChoose(List<Answer> choose) {
		this.choose = choose;
	}
	
	public long getCaseId() {
		return caseId;
	}

	public void setCaseId(long caseId) {
		this.caseId = caseId;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getTypeName() {
		return typeName;
	}

	public void setTypeName(String typeName) {
		this.typeName = typeName;
	}

	public int getRequired() {
		return required;
	}
	
	public boolean isRequired() {
		return required == 1;
	}
	
	public String getUpdateStatus() {
		return update_status;
	}

	public void setUpdateStatus(String update_status) {
		this.update_status = update_status;
	}

	public void setRequired(int required) {
		this.required = required;
	}

	public boolean isAnswered() { //该题答没答
		return answer.size() > 0 && choose.size() > 0;
	}
	
	public boolean isCorrect() { //答对了没
		if(choose.size() <= 0 || answer.size() <= 0) {
			return false;
		}
		for(Answer ans : answer) {
			if(ans.isCorrect() && !choose.contains(ans)) {
				return false;
			}
			if(!ans.isCorrect() && choose.contains(ans)) {
				return false;
			}
		}
		return true;
	}
	
	public static final Parcelable.Creator<Question> CREATOR = new Creator<Question>() {
		
		@Override  
		public Question createFromParcel(Parcel source) {  
			Question q = new Question();
			q.setId(source.readLong());
			q.setSubjectId(source.readLong());
			q.setCaseId(source.readLong());
			q.setType(source.readString());
			q.setTypeName(source.readString());
			q.setRequired(source.readInt());
			q.setContent(source.readString());
			source.readList(q.getAnswer(), Answer.class.getClassLoader());
			source.readList(q.getChoose(), Answer.class.getClassLoader());
			return q;  
		}

		@Override
		public Question[] newArray(int arg0) {
			return new Question[arg0];
		}
	};

	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void writeToParcel(Parcel arg0, int arg1) {
		arg0.writeLong(id);
		arg0.writeLong(subjectId);
		arg0.writeLong(caseId);
		arg0.writeString(type);
		arg0.writeString(typeName);
		arg0.writeInt(required);
		arg0.writeString(content);
		arg0.writeList(answer);
		arg0.writeList(choose);
	}
	
}
